package cs131.pa1.filter.sequential;

import cs131.pa1.filter.Filter;
import cs131.pa1.filter.Message;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

//why do we need isDone for this class?
public class WcCommand extends SequentialFilter{
	public final static String COMMAND = "wc";

	private List<String> commands;
	private int lineCount;
	private int wordCount;
	private int charCount;

	private String option ="-a";

	public WcCommand(List<String> commands) {
		this.commands = commands;
	}

	public void process(){
		if (isInterruption()){
			return;
		}
		String fileName = null;

		if(prev != null){
			if(input == null || input.size() == 0){
				return;
			}
		} else {
			if(commands.size() == 1){
				System.out.print(Message.REQUIRES_INPUT.with_parameter(COMMAND));
				return ;
			}
		}

		if(commands.size()==2) {
			fileName = commands.get(1);
		}else if(commands.size()==3){
			option = commands.get(1);
			fileName = commands.get(2);
		}

		if(fileName!=null){
			File file = new File(SequentialREPL.currentWorkingDirectory + File.separator + fileName);
			try {
				BufferedReader in = new BufferedReader(new FileReader(SequentialREPL.currentWorkingDirectory + File.separator + file));
				String line = null;
				while((line = in.readLine())!=null) {
					input.add(line);
				}
				in.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(output == null){
			output = new LinkedList<String>();
		}
		if(input==null){
				System.out.print(cs131.pa1.filter.Message.REQUIRES_INPUT.with_parameter(COMMAND));
				return;
		}
		while (!input.isEmpty()){
			String line = input.poll();
			processLine(line);
		}
		printReult();
	}

	private void printReult(){
		if(next==null){
			if("-w".equals(option)) {
				System.out.println(wordCount);
				return;
			}
			if("-l".equals(option)) {
				System.out.println(lineCount);
				return;
			}
			if("-c".equals(option)) {
				System.out.println(charCount);
				return;
			}
			if("-a".equals(option)) {
				System.out.print(lineCount);
				System.out.print(" ");
				System.out.print(wordCount);
				System.out.print(" ");
				System.out.println(charCount);
				return;
			}
		}else{
			if("-w".equals(option)) {
				output.add(String.valueOf(wordCount));
				return;
			}
			if("-l".equals(option)) {
				output.add(String.valueOf(lineCount));
				return;
			}
			if("-c".equals(option)) {
				output.add(String.valueOf(charCount));
				return;
			}
			if("-a".equals(option)) {
				StringBuilder sb = new StringBuilder();
				sb.append(lineCount).append(" ").append(wordCount).append(" ").append(charCount);
				output.add(sb.toString());
				return;
			}
		}
	}
	@Override
	protected String processLine(String line) {
		if(line!=null) {
			lineCount++;
		}
		charCount+=line.length();
		try {
			Scanner s = new Scanner(line);
			while (s.hasNext()) {
				s.next();
				wordCount++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
